/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package botobjects;

import interfaces.BotObject;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import tools.StringTools;
import database.DBConnectionFactory;

public class Link implements BotObject {
   /**
    * Removes the specified link
    * 
    * @param args
    *           Specifies the link to remove
    * @return true if the link was successfully deleted
    */
   public static boolean deleteFromDB(final String... args) {
	  boolean out = false;
	  if (args.length < 1)
		 return out;
	  PreparedStatement ps = null;
	  try {
		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("DELETE FROM link WHERE name = ?");
		 ps.setString(1, args[0]);

		 if (ps.executeUpdate() == 1) {
			out = true;
		 }
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return out;
   }

   /**
    * Returns a link
    * 
    * @param link
    * @return A Link objects. Returns null if no link is found
    */
   public static Link getLink(final String link) {
	  if (StringTools.isNullOrEmpty(link))
		 return null;
	  Link out = null;
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  try {
		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("SELECT * FROM link WHERE name = ?");
		 ps.setString(1, link);
		 rs = ps.executeQuery();

		 if (rs.next()) {
			out = new Link(rs.getString("name"), rs.getString("link"));
		 }
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			rs.close();
		 } catch (final Exception e) {
		 }
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return out;
   }

   /**
    * Returns a list of all links
    * 
    * @return a List of Link Objects
    */
   public static List<Link> getLinks() {
	  List<Link> out = new LinkedList<Link>();
	  PreparedStatement ps = null;
	  ResultSet rs = null;
	  try {
		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("SELECT * FROM link");
		 rs = ps.executeQuery();

		 while (rs.next()) {
			out.add(new Link(rs.getString("name"), rs.getString("link")));
		 }
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			rs.close();
		 } catch (final Exception e) {
		 }
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return out;
   }

   private String name = "";

   private String link = "";

   public Link(final String name, final String link) {
	  this.name = name;
	  this.link = link;
   }

   @Override
   public List<String> getIRCOutput(final int outputType, final String... args) {
	  List<String> out = new LinkedList<String>();
	  if (StringTools.isNullOrEmpty(getLink()) || StringTools.isNullOrEmpty(getName()))
		 return out;
	  out.add(getName() + " - " + getLink());
	  return out;
   }

   public String getLink() {
	  return link;
   }

   public String getName() {
	  return name;
   }

   @Override
   public Map<String, Object> getRPCOutput(final int... includes) {
	  Map<String, Object> out = new HashMap<String, Object>();
	  if (StringTools.isNullOrEmpty(getLink()) || StringTools.isNullOrEmpty(getName()))
		 return out;
	  out.put("Name", getName());
	  out.put("Link", getLink());
	  return out;
   }

   @Override
   public boolean saveToDB() {
	  boolean out = false;
	  if (StringTools.isNullOrEmpty(getLink()) || StringTools.isNullOrEmpty(getName()))
		 return out;
	  PreparedStatement ps = null;
	  try {
		 deleteFromDB(getName());

		 ps = DBConnectionFactory.getConnectionHandler().getConnection().prepareStatement("INSERT INTO link VALUES(?,?)");
		 ps.setString(1, getName());
		 ps.setString(2, getLink());
		 if (ps.executeUpdate() == 1) {
			out = true;
		 }
	  } catch (final SQLException e) {
		 Logger.getLogger("Error").error("", e);
	  } finally {
		 try {
			ps.close();
		 } catch (final Exception e) {
		 }
	  }
	  return out;
   }
}
